package twice.class_digui;

public class Class17_BobDie {
    public static double livePosibility1(int row, int col, int k, int N, int M) {
        return (double) process(row, col, k, N, M) / Math.pow(4, k);
    }
    // 目前在row，col位置，还有rest步要走，走完了如果还在棋盘中就获得1个生存点，返回总的生存点数
    public static int process(int row,int col,int k,int n,int m){
        if (row < 0 || row == n || col < 0 || col == m) {
            return 0;
        }
        if(k == 0){
            return 1;
        }
        return  process(row + 1,col,k - 1, n,m) +
                process(row - 1,col,k - 1, n,m) +
                process(row,col + 1,k - 1, n,m) +
                process(row,col - 1,k - 1, n,m);

    }

    public static double livePosibility2(int row, int col, int k, int N, int M){
        long[][][] dp = new long[N][M][k + 1];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                dp[i][j][0] = 1;
            }
        }
        for(int rest = 1;rest <= k;rest++){
            for(int r = 0;r < N;r++){
                for(int c = 0; c < M;c++){
                    dp[r][c][rest] = pick(dp, N, M, r - 1, c, rest - 1);
                    dp[r][c][rest] += pick(dp, N, M, r + 1, c, rest - 1);
                    dp[r][c][rest] += pick(dp, N, M, r, c - 1, rest - 1);
                    dp[r][c][rest] += pick(dp, N, M, r, c + 1, rest - 1);
                }
            }
        }
        return (double) dp[row][col][k] / Math.pow(4, k);
    }
    public static long pick(long[][][] dp, int N, int M, int r, int c, int rest) {
        if (r < 0 || r == N || c < 0 || c == M) {
            return 0;
        }
        return dp[r][c][rest];
    }
    public static void main(String[] args) {
        System.out.println(livePosibility1(6, 6, 10, 50, 50));
        System.out.println(livePosibility2(6, 6, 10, 50, 50));
    }
}
